﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.Localization;
    using ICSharpCode.SharpZipLib.Zip;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Xml;

    public class Import
    {
        private Hashtable _CatTransTab;
        private Hashtable _DocTransTab;
        private Hashtable _ImageTransTab;
        private Hashtable _ModelTransTab;
        private Hashtable _OptionTransTab;
        private Hashtable _OptionVTransTab;
        private Hashtable _ProdTransTab;
        private Hashtable _RelatedTransTab;
        private Hashtable _ShipMethodTransTab;

        private string BuildReport(string strMsg = "")
        {
            string str2 = "";
            str2 = (str2 + "Imported: " + Conversions.ToString(this._ProdTransTab.Count) + " Products<br/>") + "Imported: " + Conversions.ToString(this._CatTransTab.Count) + " Categories<br/>";
            if (strMsg != "")
            {
                str2 = str2 + "<br/><br/><br/>" + strMsg;
            }
            return str2;
        }

        private void buildTransTables(int PortalID, XmlDataDocument xmlDoc)
        {
            IEnumerator enumerator;
            LocaleCollection supportedLocales = DotNetNuke.Services.Localization.Localization.GetSupportedLocales();
            NB_Store_CategoriesInfo info = null;
            this._ProdTransTab = new Hashtable();
            this._ModelTransTab = new Hashtable();
            this._ImageTransTab = new Hashtable();
            this._OptionTransTab = new Hashtable();
            this._OptionVTransTab = new Hashtable();
            this._CatTransTab = new Hashtable();
            this._DocTransTab = new Hashtable();
            this._RelatedTransTab = new Hashtable();
            this._ShipMethodTransTab = new Hashtable();
            try
            {
                enumerator = supportedLocales.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string innerXml;
                    XmlNode current;
                    IEnumerator enumerator2;
                    IEnumerator enumerator3;
                    IEnumerator enumerator4;
                    IEnumerator enumerator5;
                    IEnumerator enumerator6;
                    IEnumerator enumerator7;
                    IEnumerator enumerator8;
                    IEnumerator enumerator9;
                    string lang = Conversions.ToString(enumerator.Current);
                    XmlNodeList list = xmlDoc.SelectNodes("root/products/" + lang + "/P/NB_Store_ProductsInfo");
                    try
                    {
                        enumerator2 = list.GetEnumerator();
                        while (enumerator2.MoveNext())
                        {
                            current = (XmlNode) enumerator2.Current;
                            innerXml = current.SelectSingleNode("ProductID").InnerXml;
                            if (!this._ProdTransTab.ContainsKey(innerXml))
                            {
                                this._ProdTransTab.Add(innerXml, "-1");
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator2 is IDisposable)
                        {
                            (enumerator2 as IDisposable).Dispose();
                        }
                    }
                    list = xmlDoc.SelectNodes("root/products/" + lang + "/P/M/NB_Store_ModelInfo");
                    try
                    {
                        enumerator3 = list.GetEnumerator();
                        while (enumerator3.MoveNext())
                        {
                            current = (XmlNode) enumerator3.Current;
                            innerXml = current.SelectSingleNode("ModelID").InnerXml;
                            if (!this._ModelTransTab.ContainsKey(innerXml))
                            {
                                this._ModelTransTab.Add(innerXml, "-1");
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator3 is IDisposable)
                        {
                            (enumerator3 as IDisposable).Dispose();
                        }
                    }
                    list = xmlDoc.SelectNodes("root/products/" + lang + "/P/I/NB_Store_ProductImageInfo");
                    try
                    {
                        enumerator4 = list.GetEnumerator();
                        while (enumerator4.MoveNext())
                        {
                            current = (XmlNode) enumerator4.Current;
                            innerXml = current.SelectSingleNode("ImageID").InnerXml;
                            if (!this._ImageTransTab.ContainsKey(innerXml))
                            {
                                this._ImageTransTab.Add(innerXml, "-1");
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator4 is IDisposable)
                        {
                            (enumerator4 as IDisposable).Dispose();
                        }
                    }
                    list = xmlDoc.SelectNodes("root/products/" + lang + "/P/D/NB_Store_ProductDocInfo");
                    try
                    {
                        enumerator5 = list.GetEnumerator();
                        while (enumerator5.MoveNext())
                        {
                            current = (XmlNode) enumerator5.Current;
                            innerXml = current.SelectSingleNode("DocID").InnerXml;
                            if (!this._DocTransTab.ContainsKey(innerXml))
                            {
                                this._DocTransTab.Add(innerXml, "-1");
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator5 is IDisposable)
                        {
                            (enumerator5 as IDisposable).Dispose();
                        }
                    }
                    list = xmlDoc.SelectNodes("root/products/" + lang + "/P/R/NB_Store_ProductRelatedInfo");
                    try
                    {
                        enumerator6 = list.GetEnumerator();
                        while (enumerator6.MoveNext())
                        {
                            current = (XmlNode) enumerator6.Current;
                            innerXml = current.SelectSingleNode("RelatedID").InnerXml;
                            if (!this._RelatedTransTab.ContainsKey(innerXml))
                            {
                                this._RelatedTransTab.Add(innerXml, "-1");
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator6 is IDisposable)
                        {
                            (enumerator6 as IDisposable).Dispose();
                        }
                    }
                    list = xmlDoc.SelectNodes("root/products/" + lang + "/P/options/O/NB_Store_OptionInfo");
                    try
                    {
                        enumerator7 = list.GetEnumerator();
                        while (enumerator7.MoveNext())
                        {
                            current = (XmlNode) enumerator7.Current;
                            innerXml = current.SelectSingleNode("OptionID").InnerXml;
                            if (!this._OptionTransTab.ContainsKey(innerXml))
                            {
                                this._OptionTransTab.Add(innerXml, "-1");
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator7 is IDisposable)
                        {
                            (enumerator7 as IDisposable).Dispose();
                        }
                    }
                    list = xmlDoc.SelectNodes("root/products/" + lang + "/P/options/O/OV/NB_Store_OptionValueInfo");
                    try
                    {
                        enumerator8 = list.GetEnumerator();
                        while (enumerator8.MoveNext())
                        {
                            current = (XmlNode) enumerator8.Current;
                            innerXml = current.SelectSingleNode("OptionValueID").InnerXml;
                            if (!this._OptionVTransTab.ContainsKey(innerXml))
                            {
                                this._OptionVTransTab.Add(innerXml, "-1");
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator8 is IDisposable)
                        {
                            (enumerator8 as IDisposable).Dispose();
                        }
                    }
                    if (info == null)
                    {
                        info = this.CreateDefaultImportCategory(PortalID, lang);
                    }
                    list = xmlDoc.SelectNodes("root/categories/" + lang + "/NB_Store_CategoriesInfo");
                    try
                    {
                        enumerator9 = list.GetEnumerator();
                        while (enumerator9.MoveNext())
                        {
                            current = (XmlNode) enumerator9.Current;
                            innerXml = current.SelectSingleNode("CategoryID").InnerXml;
                            if (!this._CatTransTab.ContainsKey(innerXml))
                            {
                                this._CatTransTab.Add(innerXml, info.CategoryID.ToString());
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator9 is IDisposable)
                        {
                            (enumerator9 as IDisposable).Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        private NB_Store_CategoriesInfo createCategoryByName(int PortalID, CategoryArrayList CatImpArray, string QualifiedName, string Lang)
        {
            CategoryController controller = new CategoryController();
            NB_Store_CategoriesInfo objInfo = null;
            NB_Store_CategoriesInfo catByName = null;
            string[] strArray = QualifiedName.Split(new char[] { '/' });
            string catQualifiedName = "";
            LocaleCollection supportedLocales = DotNetNuke.Services.Localization.Localization.GetSupportedLocales();
            int num2 = 0;
            CategoryArrayList list = new CategoryArrayList(PortalID);
            int upperBound = strArray.GetUpperBound(0);
            for (int i = 0; i <= upperBound; i++)
            {
                catQualifiedName = (catQualifiedName + "/" + strArray[i].ToString()).TrimStart(new char[] { '/' });
                catByName = CatImpArray.GetCatByName(catQualifiedName, Lang);
                if (catByName != null)
                {
                    int categoryID = catByName.CategoryID;
                    objInfo = list.GetCatByName(catQualifiedName, Lang);
                    if (objInfo == null)
                    {
                        IEnumerator enumerator;
                        objInfo = new NB_Store_CategoriesInfo {
                            CategoryID = -1,
                            PortalID = PortalID,
                            Archived = catByName.Archived,
                            CategoryDesc = catByName.CategoryDesc,
                            CategoryName = catByName.CategoryName,
                            CreatedByUser = Conversions.ToString(0),
                            CreatedDate = DateAndTime.Now,
                            Lang = Lang,
                            ListOrder = catByName.ListOrder,
                            Message = catByName.Message,
                            ParentCategoryID = num2,
                            ParentName = catByName.ParentName,
                            ProductCount = 0
                        };
                        objInfo = controller.UpdateObjCategories(objInfo);
                        try
                        {
                            enumerator = supportedLocales.GetEnumerator();
                            while (enumerator.MoveNext())
                            {
                                string lang = Conversions.ToString(enumerator.Current);
                                catByName = CatImpArray.GetCat(categoryID, lang);
                                if (catByName != null)
                                {
                                    objInfo.Lang = lang;
                                    objInfo.CategoryName = catByName.CategoryName;
                                    objInfo.CategoryDesc = catByName.CategoryDesc;
                                    objInfo.Message = catByName.Message;
                                    objInfo = controller.UpdateObjCategories(objInfo);
                                }
                            }
                        }
                        finally
                        {
                            if (enumerator is IDisposable)
                            {
                                (enumerator as IDisposable).Dispose();
                            }
                        }
                        list = new CategoryArrayList(PortalID);
                    }
                    num2 = objInfo.CategoryID;
                    this._CatTransTab[categoryID.ToString()] = objInfo.CategoryID.ToString();
                }
            }
            return objInfo;
        }

        private NB_Store_CategoriesInfo CreateDefaultImportCategory(int PortalID, string Lang)
        {
            CategoryController controller = new CategoryController();
            NB_Store_CategoriesInfo catByName = new CategoryArrayList(PortalID).GetCatByName("Imported", Lang);
            if (catByName == null)
            {
                catByName = new NB_Store_CategoriesInfo {
                    Archived = true,
                    CategoryDesc = "Products Imported",
                    CategoryID = -1,
                    CategoryName = "Imported",
                    CreatedByUser = Conversions.ToString(0),
                    CreatedDate = DateAndTime.Now,
                    Lang = Lang,
                    ListOrder = 1,
                    Message = "",
                    ParentCategoryID = 0,
                    ParentName = "",
                    PortalID = PortalID,
                    ProductCount = 0,
                    Hide = true
                };
                catByName = controller.UpdateObjCategories(catByName);
                controller.CopyToLanguages(catByName, true);
            }
            return catByName;
        }

        public string ImportCategories(PortalSettings PS, string ImportFile)
        {
            string str2 = "";
            try
            {
                IEnumerator enumerator;
                XmlDataDocument xmlDoc = new XmlDataDocument();
                LocaleCollection supportedLocales = DotNetNuke.Services.Localization.Localization.GetSupportedLocales();
                NB_Store_CategoriesInfo info2 = null;
                ProductController controller = new ProductController();
                NB_Store_CategoriesInfo catByName = null;
                xmlDoc.Load(ImportFile);
                CategoryArrayList catImpArray = new CategoryArrayList(xmlDoc);
                CategoryArrayList list = new CategoryArrayList(PS.PortalId);
                string lang = "";
                try
                {
                    enumerator = supportedLocales.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        IEnumerator enumerator2;
                        string str5 = Conversions.ToString(enumerator.Current);
                        XmlNodeList list3 = xmlDoc.SelectNodes("root/categories/" + str5 + "/NB_Store_CategoriesInfo");
                        try
                        {
                            enumerator2 = list3.GetEnumerator();
                            while (enumerator2.MoveNext())
                            {
                                XmlNode current = (XmlNode) enumerator2.Current;
                                if (lang == "")
                                {
                                    lang = str5;
                                }
                                string catQualifiedName = catImpArray.GetCatQualifiedName(Conversions.ToInteger(current["CategoryID"].InnerXml), str5);
                                if (catQualifiedName != "")
                                {
                                    catByName = list.GetCatByName(catQualifiedName, str5);
                                    if (catByName == null)
                                    {
                                        info2 = this.createCategoryByName(PS.PortalId, catImpArray, catQualifiedName, str5);
                                    }
                                    else
                                    {
                                        info2 = catImpArray.GetCatByName(catQualifiedName, str5);
                                        this._CatTransTab[info2.CategoryID.ToString()] = catByName.CategoryID.ToString();
                                    }
                                }
                            }
                        }
                        finally
                        {
                            if (enumerator2 is IDisposable)
                            {
                                (enumerator2 as IDisposable).Dispose();
                            }
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
                if (lang == "")
                {
                    lang = SharedFunctions.GetMerchantCulture(PS.PortalId);
                }
                controller.CategoryLangValidation(PS.PortalId, lang);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                str2 = exception1.ToString();
                ProjectData.ClearProjectError();
            }
            return str2;
        }

        public string ImportProductDocs(PortalSettings PS, string ImportZipFile)
        {
            SharedFunctions.CreateDir(PS, "productdocs");
            return this.UnZipImportFile(ImportZipFile, PS.HomeDirectoryMapPath + @"productdocs\", PS);
        }

        public string ImportProductImages(PortalSettings PS, string ImportZipFile)
        {
            SharedFunctions.CreateDir(PS, "productimages");
            return this.UnZipImportFile(ImportZipFile, PS.HomeDirectoryMapPath + @"productimages\", PS);
        }

        public string ImportProducts(PortalSettings PS, string ImportFile, bool UpdateExistingProductsFlg, bool CreateCategoriesFlg)
        {
            string strMsg = "";
            try
            {
                bool flag;
                XmlNode node;
                XmlNodeList list;
                XmlNode current;
                XmlNodeList list5;
                IEnumerator enumerator;
                IEnumerator enumerator12;
                XmlDataDocument xmlDoc = new XmlDataDocument();
                NB_Store_ProductsInfo objInfo = new NB_Store_ProductsInfo();
                NB_Store_ModelInfo info3 = new NB_Store_ModelInfo();
                NB_Store_ProductImageInfo info2 = new NB_Store_ProductImageInfo();
                NB_Store_ProductDocInfo info = new NB_Store_ProductDocInfo();
                NB_Store_OptionInfo info5 = new NB_Store_OptionInfo();
                NB_Store_OptionValueInfo info6 = new NB_Store_OptionValueInfo();
                ShipController controller2 = new ShipController();
                NB_Store_ShippingRatesInfo info10 = new NB_Store_ShippingRatesInfo();
                NB_Store_ProductRelatedInfo info9 = new NB_Store_ProductRelatedInfo();
                ProductController controller = new ProductController();
                LocaleCollection validLocales = SharedFunctions.GetValidLocales();
                xmlDoc.Load(ImportFile);
                this.buildTransTables(PS.PortalId, xmlDoc);
                if (CreateCategoriesFlg)
                {
                    strMsg = this.ImportCategories(PS, ImportFile);
                    if (strMsg != "")
                    {
                        return strMsg;
                    }
                }
                string lang = "";
                try
                {
                    enumerator = validLocales.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        IEnumerator enumerator2;
                        string str5 = Conversions.ToString(enumerator.Current);
                        list5 = xmlDoc.SelectNodes("root/products/" + str5 + "/P");
                        try
                        {
                            enumerator2 = list5.GetEnumerator();
                            while (enumerator2.MoveNext())
                            {
                                current = (XmlNode) enumerator2.Current;
                                try
                                {
                                    XmlNode node2;
                                    IEnumerator enumerator3;
                                    IEnumerator enumerator4;
                                    IEnumerator enumerator5;
                                    IEnumerator enumerator7;
                                    IEnumerator enumerator9;
                                    XmlNodeList list2 = current.SelectNodes("M/NB_Store_ModelInfo");
                                    try
                                    {
                                        enumerator3 = list2.GetEnumerator();
                                        while (enumerator3.MoveNext())
                                        {
                                            node2 = (XmlNode) enumerator3.Current;
                                            info3 = (NB_Store_ModelInfo) XmlUtils.Deserialize(node2.OuterXml, info3.GetType());
                                        }
                                    }
                                    finally
                                    {
                                        if (enumerator3 is IDisposable)
                                        {
                                            (enumerator3 as IDisposable).Dispose();
                                        }
                                    }
                                    if (lang == "")
                                    {
                                        lang = str5;
                                    }
                                    objInfo = (NB_Store_ProductsInfo) XmlUtils.Deserialize(current.SelectSingleNode("NB_Store_ProductsInfo").OuterXml, objInfo.GetType());
                                    objInfo.PortalID = PS.PortalId;
                                    int productID = objInfo.ProductID;
                                    if (UpdateExistingProductsFlg & (objInfo.ProductRef != ""))
                                    {
                                        NB_Store_ProductsInfo info8 = controller.GetProductByRef(PS.PortalId, objInfo.ProductRef, str5);
                                        if (info8 != null)
                                        {
                                            objInfo.ProductID = info8.ProductID;
                                        }
                                        else
                                        {
                                            objInfo.ProductID = Conversions.ToInteger(this._ProdTransTab[productID.ToString()]);
                                        }
                                    }
                                    else
                                    {
                                        objInfo.ProductID = Conversions.ToInteger(this._ProdTransTab[productID.ToString()]);
                                    }
                                    objInfo = controller.UpdateObjProduct(objInfo);
                                    this._ProdTransTab[productID.ToString()] = objInfo.ProductID.ToString();
                                    if (CreateCategoriesFlg)
                                    {
                                        this.UpdateCategoryXref(objInfo, current.SelectNodes("C/NB_Store_ProductCategoryInfo"));
                                    }
                                    list2 = current.SelectNodes("M/NB_Store_ModelInfo");
                                    try
                                    {
                                        enumerator4 = list2.GetEnumerator();
                                        while (enumerator4.MoveNext())
                                        {
                                            node2 = (XmlNode) enumerator4.Current;
                                            info3 = (NB_Store_ModelInfo) XmlUtils.Deserialize(node2.OuterXml, info3.GetType());
                                            info3.ProductID = objInfo.ProductID;
                                            info3.PortalID = PS.PortalId;
                                            int modelID = info3.ModelID;
                                            if (UpdateExistingProductsFlg)
                                            {
                                                NB_Store_ModelInfo info4 = controller.GetModelByRef(objInfo.ProductID, info3.ModelRef, str5);
                                                if (info4 != null)
                                                {
                                                    info3.ModelID = info4.ModelID;
                                                }
                                                else
                                                {
                                                    info3.ModelID = Conversions.ToInteger(this._ModelTransTab[modelID.ToString()]);
                                                }
                                            }
                                            else
                                            {
                                                info3.ModelID = Conversions.ToInteger(this._ModelTransTab[modelID.ToString()]);
                                            }
                                            this._ModelTransTab[modelID.ToString()] = controller.UpdateObjModel(info3).ToString();
                                            info3.ModelID = Conversions.ToInteger(this._ModelTransTab[modelID.ToString()]);
                                            decimal num8 = new decimal();
                                            NB_Store_ShippingRatesInfo info11 = controller2.GetShippingRateByObjID(info3.PortalID, info3.ModelID, "PRD", -1);
                                            if (info11 != null)
                                            {
                                                info11.ProductWeight = info3.Weight;
                                                info11.ProductHeight = info3.Height;
                                                info11.ProductLength = info3.Length;
                                                info11.ProductWidth = info3.Width;
                                            }
                                            else
                                            {
                                                info11 = new NB_Store_ShippingRatesInfo {
                                                    Description = "",
                                                    Disable = false,
                                                    ItemId = -1,
                                                    ProductWeight = info3.Weight,
                                                    ProductHeight = info3.Height,
                                                    ProductLength = info3.Length,
                                                    ProductWidth = info3.Width,
                                                    ObjectId = info3.ModelID,
                                                    Range1 = decimal.Zero,
                                                    Range2 = decimal.Zero,
                                                    PortalID = info3.PortalID,
                                                    ShipCost = decimal.Zero,
                                                    ShipMethodID = -1,
                                                    ShipType = "PRD"
                                                };
                                            }
                                            controller2.UpdateObjShippingRate(info11);
                                        }
                                    }
                                    finally
                                    {
                                        if (enumerator4 is IDisposable)
                                        {
                                            (enumerator4 as IDisposable).Dispose();
                                        }
                                    }
                                    list = current.SelectNodes("I/NB_Store_ProductImageInfo");
                                    flag = false;
                                    try
                                    {
                                        enumerator5 = list.GetEnumerator();
                                        while (enumerator5.MoveNext())
                                        {
                                            node = (XmlNode) enumerator5.Current;
                                            info2 = (NB_Store_ProductImageInfo) XmlUtils.Deserialize(node.OuterXml, info2.GetType());
                                            int imageID = info2.ImageID;
                                            if ((UpdateExistingProductsFlg & !flag) && Operators.ConditionalCompareObjectEqual(this._ImageTransTab[imageID.ToString()], "-1", false))
                                            {
                                                IEnumerator enumerator6;
                                                ArrayList productImageList = controller.GetProductImageList(objInfo.ProductID, str5);
                                                try
                                                {
                                                    enumerator6 = productImageList.GetEnumerator();
                                                    while (enumerator6.MoveNext())
                                                    {
                                                        NB_Store_ProductImageInfo info12 = (NB_Store_ProductImageInfo) enumerator6.Current;
                                                        controller.DeleteProductImage(info12.ImageID);
                                                    }
                                                }
                                                finally
                                                {
                                                    if (enumerator6 is IDisposable)
                                                    {
                                                        (enumerator6 as IDisposable).Dispose();
                                                    }
                                                }
                                                flag = true;
                                            }
                                            info2.ProductID = objInfo.ProductID;
                                            info2.ImageID = Conversions.ToInteger(this._ImageTransTab[imageID.ToString()]);
                                            info2.ImagePath = PS.HomeDirectoryMapPath + @"productimages\" + Path.GetFileName(info2.ImagePath);
                                            info2.ImageURL = PS.HomeDirectory + "productimages/" + Path.GetFileName(info2.ImagePath);
                                            if (Path.GetFileNameWithoutExtension(info2.ImagePath) != "")
                                            {
                                                this._ImageTransTab[imageID.ToString()] = controller.UpdateObjProductImage(info2).ToString();
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        if (enumerator5 is IDisposable)
                                        {
                                            (enumerator5 as IDisposable).Dispose();
                                        }
                                    }
                                    list = current.SelectNodes("D/NB_Store_ProductDocInfo");
                                    flag = false;
                                    try
                                    {
                                        enumerator7 = list.GetEnumerator();
                                        while (enumerator7.MoveNext())
                                        {
                                            node = (XmlNode) enumerator7.Current;
                                            info = (NB_Store_ProductDocInfo) XmlUtils.Deserialize(node.OuterXml, info.GetType());
                                            int docID = info.DocID;
                                            if ((UpdateExistingProductsFlg & !flag) && Operators.ConditionalCompareObjectEqual(this._DocTransTab[docID.ToString()], "-1", false))
                                            {
                                                IEnumerator enumerator8;
                                                ArrayList productDocList = controller.GetProductDocList(objInfo.ProductID, str5);
                                                try
                                                {
                                                    enumerator8 = productDocList.GetEnumerator();
                                                    while (enumerator8.MoveNext())
                                                    {
                                                        NB_Store_ProductDocInfo info13 = (NB_Store_ProductDocInfo) enumerator8.Current;
                                                        controller.DeleteProductDoc(info13.DocID);
                                                    }
                                                }
                                                finally
                                                {
                                                    if (enumerator8 is IDisposable)
                                                    {
                                                        (enumerator8 as IDisposable).Dispose();
                                                    }
                                                }
                                                flag = true;
                                            }
                                            info.ProductID = objInfo.ProductID;
                                            info.DocID = Conversions.ToInteger(this._DocTransTab[docID.ToString()]);
                                            info.DocPath = PS.HomeDirectoryMapPath + @"productdocs\" + Path.GetFileName(info.DocPath);
                                            if (Path.GetFileNameWithoutExtension(info.DocPath) != "")
                                            {
                                                this._DocTransTab[docID.ToString()] = controller.UpdateObjProductDoc(info).ToString();
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        if (enumerator7 is IDisposable)
                                        {
                                            (enumerator7 as IDisposable).Dispose();
                                        }
                                    }
                                    XmlNodeList list3 = current.SelectNodes("options/O");
                                    flag = false;
                                    try
                                    {
                                        enumerator9 = list3.GetEnumerator();
                                        while (enumerator9.MoveNext())
                                        {
                                            IEnumerator enumerator11;
                                            XmlNode node3 = (XmlNode) enumerator9.Current;
                                            info5 = (NB_Store_OptionInfo) XmlUtils.Deserialize(node3.SelectSingleNode("NB_Store_OptionInfo").OuterXml, info5.GetType());
                                            int optionID = info5.OptionID;
                                            if ((UpdateExistingProductsFlg & !flag) && Operators.ConditionalCompareObjectEqual(this._OptionTransTab[optionID.ToString()], "-1", false))
                                            {
                                                IEnumerator enumerator10;
                                                ArrayList optionList = controller.GetOptionList(objInfo.ProductID, str5);
                                                try
                                                {
                                                    enumerator10 = optionList.GetEnumerator();
                                                    while (enumerator10.MoveNext())
                                                    {
                                                        NB_Store_OptionInfo info14 = (NB_Store_OptionInfo) enumerator10.Current;
                                                        controller.DeleteOption(info14.OptionID);
                                                    }
                                                }
                                                finally
                                                {
                                                    if (enumerator10 is IDisposable)
                                                    {
                                                        (enumerator10 as IDisposable).Dispose();
                                                    }
                                                }
                                                flag = true;
                                            }
                                            info5.ProductID = objInfo.ProductID;
                                            info5.OptionID = Conversions.ToInteger(this._OptionTransTab[optionID.ToString()]);
                                            info5 = controller.UpdateObjOption(info5);
                                            this._OptionTransTab[optionID.ToString()] = info5.OptionID.ToString();
                                            XmlNodeList list4 = node3.SelectNodes("OV/NB_Store_OptionValueInfo");
                                            try
                                            {
                                                enumerator11 = list4.GetEnumerator();
                                                while (enumerator11.MoveNext())
                                                {
                                                    XmlNode node4 = (XmlNode) enumerator11.Current;
                                                    info6 = (NB_Store_OptionValueInfo) XmlUtils.Deserialize(node4.OuterXml, info6.GetType());
                                                    int optionValueID = info6.OptionValueID;
                                                    info6.OptionID = info5.OptionID;
                                                    info6.OptionValueID = Conversions.ToInteger(this._OptionVTransTab[optionValueID.ToString()]);
                                                    this._OptionVTransTab[optionValueID.ToString()] = controller.UpdateObjOptionValue(info6).ToString();
                                                }
                                            }
                                            finally
                                            {
                                                if (enumerator11 is IDisposable)
                                                {
                                                    (enumerator11 as IDisposable).Dispose();
                                                }
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        if (enumerator9 is IDisposable)
                                        {
                                            (enumerator9 as IDisposable).Dispose();
                                        }
                                    }
                                }
                                catch (Exception exception1)
                                {
                                    ProjectData.SetProjectError(exception1);
                                    SharedFunctions.UpdateLog("ERROR on Import: " + exception1.ToString());
                                    strMsg = strMsg + "Product Ref: \"" + current.SelectSingleNode("NB_Store_ProductsInfo/ProductRef").InnerText + "\" failed to import.<br/>";
                                    ProjectData.ClearProjectError();
                                }
                            }
                        }
                        finally
                        {
                            if (enumerator2 is IDisposable)
                            {
                                (enumerator2 as IDisposable).Dispose();
                            }
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
                if (lang == "")
                {
                    lang = SharedFunctions.GetMerchantCulture(PS.PortalId);
                }
                list5 = xmlDoc.SelectNodes("root/products/" + lang + "/P");
                try
                {
                    enumerator12 = list5.GetEnumerator();
                    while (enumerator12.MoveNext())
                    {
                        current = (XmlNode) enumerator12.Current;
                        try
                        {
                            IEnumerator enumerator13;
                            list = current.SelectNodes("R/NB_Store_ProductRelatedInfo");
                            flag = false;
                            try
                            {
                                enumerator13 = list.GetEnumerator();
                                while (enumerator13.MoveNext())
                                {
                                    node = (XmlNode) enumerator13.Current;
                                    info9 = (NB_Store_ProductRelatedInfo) XmlUtils.Deserialize(node.OuterXml, info9.GetType());
                                    int relatedID = info9.RelatedID;
                                    if ((UpdateExistingProductsFlg & !flag) && Operators.ConditionalCompareObjectEqual(this._RelatedTransTab[relatedID.ToString()], "-1", false))
                                    {
                                        controller.DeleteProductRelatedByProduct(objInfo.ProductID);
                                        flag = true;
                                    }
                                    info9.ProductID = Conversions.ToInteger(this._ProdTransTab[info9.ProductID.ToString()]);
                                    info9.RelatedProductID = Conversions.ToInteger(this._ProdTransTab[info9.RelatedProductID.ToString()]);
                                    info9.RelatedID = Conversions.ToInteger(this._RelatedTransTab[relatedID.ToString()]);
                                    this._RelatedTransTab[relatedID.ToString()] = controller.UpdateObjProductRelated(info9);
                                }
                            }
                            finally
                            {
                                if (enumerator13 is IDisposable)
                                {
                                    (enumerator13 as IDisposable).Dispose();
                                }
                            }
                        }
                        catch (Exception exception4)
                        {
                            ProjectData.SetProjectError(exception4);
                            SharedFunctions.UpdateLog("ERROR on Import: " + exception4.ToString());
                            strMsg = strMsg + "Related Products for Product Ref: \"" + current.SelectSingleNode("NB_Store_ProductsInfo/ProductRef").InnerText + "\" failed to import.<br/>";
                            ProjectData.ClearProjectError();
                        }
                    }
                }
                finally
                {
                    if (enumerator12 is IDisposable)
                    {
                        (enumerator12 as IDisposable).Dispose();
                    }
                }
                controller.ProductLangValidation(PS.PortalId, lang);
                return this.BuildReport(strMsg);
            }
            catch (Exception exception5)
            {
                ProjectData.SetProjectError(exception5);
                strMsg = strMsg + exception5.ToString();
                ProjectData.ClearProjectError();
            }
            return strMsg;
        }

        public string ImportShipping(PortalSettings PS, string ImportFile)
        {
            IEnumerator enumerator;
            IEnumerator enumerator2;
            XmlDataDocument document = new XmlDataDocument();
            string str3 = "<br/>Shipping Updated : ";
            ShipController controller = new ShipController();
            NB_Store_ShippingRatesInfo objInfo = new NB_Store_ShippingRatesInfo();
            NB_Store_ShippingMethodInfo info = new NB_Store_ShippingMethodInfo();
            int num = 0;
            document.Load(ImportFile);
            controller.ClearAllShippingMethods(PS.PortalId);
            this._ShipMethodTransTab = new Hashtable();
            XmlNodeList list = document.SelectNodes("root/shipping/method/NB_Store_ShippingMethodInfo");
            try
            {
                enumerator = list.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string key = Conversions.ToString(NewLateBinding.LateGet(NewLateBinding.LateGet(RuntimeHelpers.GetObjectValue(enumerator.Current), null, "SelectSingleNode", new object[] { "ShipMethodID" }, null, null, null), null, "InnerXml", new object[0], null, null, null));
                    if (!this._ShipMethodTransTab.ContainsKey(key))
                    {
                        this._ShipMethodTransTab.Add(key, "-1");
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            list = document.SelectNodes("root/shipping/method");
            try
            {
                enumerator2 = list.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    IEnumerator enumerator3;
                    XmlNode current = (XmlNode) enumerator2.Current;
                    info = (NB_Store_ShippingMethodInfo) XmlUtils.Deserialize(current.SelectSingleNode("NB_Store_ShippingMethodInfo").OuterXml, info.GetType());
                    int shipMethodID = info.ShipMethodID;
                    info.ShipMethodID = -1;
                    info.PortalID = PS.PortalId;
                    int num2 = controller.UpdateObjShippingMethod(info);
                    this._ShipMethodTransTab[shipMethodID] = num2;
                    XmlNodeList list2 = current.SelectNodes("rate/NB_Store_ShippingRatesInfo[ShipType!='PRD']");
                    try
                    {
                        enumerator3 = list2.GetEnumerator();
                        while (enumerator3.MoveNext())
                        {
                            XmlNode node2 = (XmlNode) enumerator3.Current;
                            objInfo = (NB_Store_ShippingRatesInfo) XmlUtils.Deserialize(node2.OuterXml, objInfo.GetType());
                            objInfo.ItemId = -1;
                            objInfo.ObjectId = -1;
                            objInfo.PortalID = PS.PortalId;
                            objInfo.ShipMethodID = num2;
                            controller.UpdateObjShippingRate(objInfo);
                            num++;
                        }
                    }
                    finally
                    {
                        if (enumerator3 is IDisposable)
                        {
                            (enumerator3 as IDisposable).Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
            return (str3 + num.ToString());
        }

        public void ImportSQLReports(int PortalID, string ImportFile, bool blnOverwrite)
        {
            XmlDataDocument document = new XmlDataDocument();
            document.Load(ImportFile);
            XmlNode content = DotNetNuke.Common.Globals.GetContent(document.OuterXml, "content/SQLReports");
            this.ImportSQLReports(PortalID, content, blnOverwrite);
        }

        public void ImportSQLReports(int PortalID, XmlNode XmlInput, bool blnOverwrite)
        {
            XmlNode node3 = XmlInput;
            SQLReportController controller = new SQLReportController();
            if (node3.SelectNodes("SQLReport") != null)
            {
                IEnumerator enumerator;
                try
                {
                    enumerator = node3.SelectNodes("SQLReport").GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        XmlNode current = (XmlNode) enumerator.Current;
                        if (current.SelectSingleNode("NB_Store_SQLReportInfo").OuterXml != null)
                        {
                            NB_Store_SQLReportInfo objInfo = new NB_Store_SQLReportInfo();
                            objInfo = (NB_Store_SQLReportInfo) XmlUtils.Deserialize(current.SelectSingleNode("NB_Store_SQLReportInfo").OuterXml, objInfo.GetType());
                            NB_Store_SQLReportInfo sQLReportByRef = controller.GetSQLReportByRef(PortalID, objInfo.ReportRef);
                            if (sQLReportByRef != null)
                            {
                                objInfo.ReportID = sQLReportByRef.ReportID;
                            }
                            else
                            {
                                objInfo.ReportID = -1;
                            }
                            if ((objInfo.ReportID == -1) | blnOverwrite)
                            {
                                XmlNode node2;
                                IEnumerator enumerator2;
                                IEnumerator enumerator3;
                                if (objInfo.ReportID >= 0)
                                {
                                    controller.DeleteSQLReport(objInfo.ReportID);
                                }
                                objInfo.PortalID = PortalID;
                                int num = controller.UpdateObjSQLReport(objInfo);
                                try
                                {
                                    enumerator2 = current.SelectNodes("SQLReportParams/*").GetEnumerator();
                                    while (enumerator2.MoveNext())
                                    {
                                        node2 = (XmlNode) enumerator2.Current;
                                        if (node2.SelectSingleNode(".") != null)
                                        {
                                            NB_Store_SQLReportParamInfo info2 = new NB_Store_SQLReportParamInfo();
                                            info2 = (NB_Store_SQLReportParamInfo) XmlUtils.Deserialize(node2.SelectSingleNode(".").OuterXml, info2.GetType());
                                            info2.ReportID = num;
                                            info2.ReportParamID = -1;
                                            controller.UpdateObjSQLReportParam(info2);
                                        }
                                    }
                                }
                                finally
                                {
                                    if (enumerator2 is IDisposable)
                                    {
                                        (enumerator2 as IDisposable).Dispose();
                                    }
                                }
                                try
                                {
                                    enumerator3 = current.SelectNodes("SQLReportXSL/*").GetEnumerator();
                                    while (enumerator3.MoveNext())
                                    {
                                        node2 = (XmlNode) enumerator3.Current;
                                        if (node2.SelectSingleNode(".") != null)
                                        {
                                            NB_Store_SQLReportXSLInfo info4 = new NB_Store_SQLReportXSLInfo();
                                            info4 = (NB_Store_SQLReportXSLInfo) XmlUtils.Deserialize(node2.SelectSingleNode(".").OuterXml, info4.GetType());
                                            info4.ReportID = num;
                                            info4.ReportXSLID = -1;
                                            controller.UpdateObjSQLReportXSL(info4);
                                        }
                                    }
                                }
                                finally
                                {
                                    if (enumerator3 is IDisposable)
                                    {
                                        (enumerator3 as IDisposable).Dispose();
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
        }

        private string UnZipImportFile(string ZipFileName, string DestFolder, PortalSettings PS)
        {
            string fileName = Path.GetFileName(ZipFileName);
            string path = "";
            byte[] b = new byte[0x800];
            int num2 = 0;
            ZipInputStream stream = new ZipInputStream(File.OpenRead(ZipFileName));
            for (ZipEntry entry = stream.GetNextEntry(); entry != null; entry = stream.GetNextEntry())
            {
                if (!entry.IsDirectory)
                {
                    path = Path.GetFileName(entry.Name);
                    if (path != "")
                    {
                        string extension = Path.GetExtension(path);
                        if ((Strings.InStr(1, "," + PS.HostSettings["FileExtensions"].ToString().ToUpper(), "," + Strings.Replace(extension.ToUpper(), ".", "", 1, -1, CompareMethod.Binary), CompareMethod.Binary) != 0) | SharedFunctions.IsImageFile(extension))
                        {
                            try
                            {
                                DirectoryInfo info = new DirectoryInfo(Path.GetDirectoryName(DestFolder + Strings.Replace(entry.Name, "/", @"\", 1, -1, CompareMethod.Binary)));
                                if (info.Exists)
                                {
                                    FileStream stream2 = File.Create(DestFolder + Strings.Replace(entry.Name, "/", @"\", 1, -1, CompareMethod.Binary), 0x800);
                                    while (true)
                                    {
                                        int count = stream.Read(b, 0, 0x800);
                                        if (count == 0)
                                        {
                                            break;
                                        }
                                        stream2.Write(b, 0, count);
                                    }
                                    stream2.Close();
                                    num2++;
                                }
                            }
                            catch (Exception exception1)
                            {
                                ProjectData.SetProjectError(exception1);
                                Exception exception = exception1;
                                if (stream != null)
                                {
                                    stream.Close();
                                }
                                string message = exception.Message;
                                ProjectData.ClearProjectError();
                                return message;
                            }
                        }
                    }
                }
            }
            stream.Close();
            return ("<br/>Imported Zip Items: " + num2.ToString());
        }

        public void UpdateCategoryXref(NB_Store_ProductsInfo objPInfo, XmlNodeList xmlNodList)
        {
            IEnumerator enumerator;
            NB_Store_ProductCategoryInfo objInfo = new NB_Store_ProductCategoryInfo();
            ProductController controller = new ProductController();
            controller.DeleteProductCategory(objPInfo.ProductID);
            try
            {
                enumerator = xmlNodList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    object objectValue = RuntimeHelpers.GetObjectValue(enumerator.Current);
                    object[] objArray = new object[2];
                    object instance = objectValue;
                    objArray[0] = RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(instance, null, "OuterXml", new object[0], null, null, null));
                    objArray[1] = objInfo.GetType();
                    object[] arguments = objArray;
                    bool[] copyBack = new bool[] { true, false };
                    if (copyBack[0])
                    {
                        NewLateBinding.LateSetComplex(instance, null, "OuterXml", new object[] { RuntimeHelpers.GetObjectValue(arguments[0]) }, null, null, true, false);
                    }
                    objInfo = (NB_Store_ProductCategoryInfo) NewLateBinding.LateGet(null, typeof(XmlUtils), "Deserialize", arguments, null, null, copyBack);
                    objInfo.CategoryID = Conversions.ToInteger(this._CatTransTab[objInfo.CategoryID.ToString()]);
                    objInfo.ProductID = objPInfo.ProductID;
                    controller.UpdateObjProductCategory(objInfo);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }
    }
}

